﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TekaMUD.Engine.Blocks;

namespace TekaMUD.Engine.Framework
{
	public class Logger
	{
		private static DebugLevel CurrentLevel
		{
			get
			{
				DebugLevel? dl = (DebugLevel?)RuntimeOptions.GetOption(RuntimeOption.DebugLevel);

				return dl.HasValue ? dl.Value : DebugLevel.Error;
			}
		}

		private static bool UseTrace
		{
			get
			{
				object o = RuntimeOptions.GetOption(RuntimeOption.UseTrace);

				if (o == null)
					return false;
				else
					return (bool)o;

			}
		}

		private static bool Activated
		{
			get
			{
				bool? b = (bool?)RuntimeOptions.GetOption(RuntimeOption.DebugActive);

				return b.HasValue && b.Value;
			}
		}

		public static void LogWarning(string message)
		{
			if (CurrentLevel >= DebugLevel.Warn)	
				LogMessageWithDate(message, ConsoleColor.Cyan);
		}

		public static void LogError(string message)
		{
			if (CurrentLevel >= DebugLevel.Error)
				LogMessageWithDate(message, ConsoleColor.Red);
		}

		public static void LogInfo(string message)
		{
			if (CurrentLevel >= DebugLevel.Info)
				LogMessageWithDate(message, ConsoleColor.White);
		}

		private static void LogMessageWithDate(string msg, ConsoleColor cc)
		{
			if (!Activated)
				return;

			if (UseTrace)
			{
				System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") + " # " + msg);
			}
			else
			{
				System.Console.ForegroundColor = cc;
				System.Console.WriteLine(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") + " # " + msg);
				System.Console.ForegroundColor = ConsoleColor.White;
			}
		}

		public static void LogDebug(string message)
		{
			if (CurrentLevel >= DebugLevel.Debug)
				LogMessageWithDate(message, ConsoleColor.Magenta);
		}
	}

	/// <summary>
	/// Levels of debug.
	/// </summary>
	public enum DebugLevel
	{
		Error = 1,
		Warn = 2,
		Info = 3,
		Debug = 4 
	}
}